<template>
  <div>
    <el-row type="flex" justify="end">
      <el-select v-model="year" size="small" style="width:100px" @change="dataChange">
        <el-option v-for="i in yearList" :key="i" :label="i" :value="i" />
      </el-select>
      <el-select v-model="month" size="small" style="width:100px;margin-left:20px" @change="dataChange">
        <el-option v-for="i in 12" :key="i" :label="i" :value="i" />
      </el-select>
    </el-row>
    <el-calendar v-if="isShow" v-model="curDate">
      <template v-slot:dateCell="{date,data}" class="content">
        <div class="date-content">
          <span class="text"> {{ data.day|formatDay }}</span>
          <span v-if="isweek(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
const curDate = new Date() // 当前日期对象
const year = curDate.getFullYear() // 当前年份
const month = curDate.getMonth() + 1// 当前年份
const yearList = [] // 保存年份列表
for (let i = 0; i < 11; i++) {
  yearList.push(year - 5 + i)
}
export default {
  filters: {
    // 格式化日历时间
    formatDay(val) {
      return parseInt(val.split('-')[2])
    }

  },
  data() {
    return {
      curDate,
      year,
      month,
      yearList: yearList,
      isShow: true
    }
  },
  watch: {
    // 日历关联年月
    curDate(val) {
      // console.log(val)
      this.year = val.getFullYear()
      this.month = val.getMonth() + 1
    },
    // 侦听语言, 侦听对象中的某个属性，写法如下
    '$i18n.locale'() {
      // 解决日历组件 多语言切换不实时更新问题
      this.isShow = false
      this.$nextTick(() => {
        this.isShow = true
      })
    }
  },
  methods: {
    dataChange() {
      this.curDate = new Date(this.year + '-' + this.month)
    },
    isweek(date) {
      return date.getDay() === 0 || date.getDay() === 6
    }
  }
}
</script>

<style lang='scss' scoped>
 ::v-deep .el-calendar-day {
  height:  auto;
  /* margin-right: 10px; */
 }
 ::v-deep .el-calendar-table__row td,::v-deep .el-calendar-table tr td:first-child,  ::v-deep .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 1px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
 ::v-deep .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
 ::v-deep .el-calendar__header {
   display: none
 }
</style>

